package com.cl.blog.web.client.community;

import cn.hutool.core.io.resource.Resource;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.cl.blog.common.Const;
import com.cl.blog.common.Result;
import com.cl.blog.entity.BCommunity;
import com.cl.blog.entity.BCommunityReview;
import com.cl.blog.entity.BUser;
import com.cl.blog.mapper.BUserMapper;
import com.cl.blog.mq.DelayMessageSender;
import com.cl.blog.service.BCommunityReviewService;
import com.cl.blog.service.BCommunityService;
import com.cl.blog.util.JwtUtil;
import com.cl.blog.vo.CommunityAddVo;
import com.cl.blog.vo.CommunitySearchVo;
import com.github.houbb.sensitive.word.api.IWordTag;
import com.github.houbb.sensitive.word.bs.SensitiveWordBs;
import com.github.houbb.sensitive.word.support.ignore.SensitiveWordCharIgnores;
import com.github.houbb.sensitive.word.support.tag.WordTags;
import io.jsonwebtoken.Claims;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

import static com.cl.blog.config.RabbitMqConfig.*;

/**
 * @Author 付
 * @Date 2024-04-06 11:35
 * @Version 1.0
 * @description
 * @since 1.0
 */
@RestController
@RequestMapping("/user/community")
public class CommunityController {

	@Autowired
	BCommunityService bCommunityService;
	@Autowired
	RabbitTemplate rabbitTemplate;
	@Autowired
	JwtUtil jwtUtil;

	@PostMapping("/insert")
	public Result insertCommunity(@RequestBody CommunityAddVo form, HttpServletRequest request) {
		String token = request.getHeader("token");
		boolean b = bCommunityService.addCommunity(form, token);

		return Result.success();
	}

	/**
	 * 敏感词检测
	 * @param form
	 * @return
	 * @throws IOException
	 */
	@PostMapping("/forbidden")
	public Result sh(@RequestBody CommunityAddVo form) throws IOException {
		ClassPathResource resource = new ClassPathResource("dict_tag_test.txt");
		String path = resource.getFile().getPath();
		IWordTag wordTag = WordTags.file(path);

		SensitiveWordBs sensitiveWordBs = SensitiveWordBs.newInstance()
				.wordTag(wordTag)
				.charIgnore(SensitiveWordCharIgnores.specialChars())
				.init();
		List<String> all = sensitiveWordBs.findAll(form.getContent());

		return Result.success(all);
	}

	@GetMapping("/list")
	public Result list(Integer type) {
		List<BCommunity> list = bCommunityService.selectAll(type);

		return Result.success(list);
	}

	@PostMapping("/search")
	public Result search(@RequestBody CommunitySearchVo searchVo) {
		List<BCommunity> list = bCommunityService.search(searchVo);
		return Result.success(list);
	}

	@GetMapping("/detail")
	public Result getDetail(Integer id) {
		BCommunity bCommunity = bCommunityService.selectById(id);
		return Result.success(bCommunity);
	}

	@GetMapping("/like")
	public Result like(Integer id) {
		boolean id1 = bCommunityService.update(new UpdateWrapper<BCommunity>()
				.eq("id", id)
				.setSql("like_count = like_count + 1"));
		if (!id1) {
			return Result.error("稍后再试");
		}
		return Result.success();
	}

	@GetMapping("/user_list")
	public Result getListByUser(HttpServletRequest request) {
		String token = request.getHeader("token");
		Claims claim = jwtUtil.getClaimByToken(token, Const.GRADE_USER);
		List<BCommunity> userId = bCommunityService.list(new QueryWrapper<BCommunity>().eq("user_id", Integer.valueOf(claim.getSubject())));
		return Result.success(userId);
	}

}
